package com.coder520.mamabike.controller;

import com.coder520.mamabike.bo.UserBo;
import com.coder520.mamabike.common.Const;
import com.coder520.mamabike.common.ResponseCode;
import com.coder520.mamabike.common.ServerResponse;
import com.coder520.mamabike.common.exception.MyException;
import com.coder520.mamabike.dao.cache.RedisDao;
import com.coder520.mamabike.pojo.LoginInfo;
import com.coder520.mamabike.pojo.User;
import com.coder520.mamabike.service.IUserService;
import com.coder520.mamabike.utils.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by ASUS on 2017/8/9.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService iUserService;

    @Autowired
    private RedisDao redisDao;

    /**
     * 用户登录
     *
     * @param loginInfo
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServerResponse login(@RequestBody LoginInfo loginInfo) {
        return iUserService.login(loginInfo);
    }

    /**
     * 修改nickname
     */
    @RequestMapping("/update_nickname")
    public ServerResponse updateNickName(HttpServletRequest request, @RequestBody User user) {
        String token = request.getHeader(Const.CURRENT_USER);
        if (StringUtils.isNotBlank(token)) {
            //防止横向越权
            UserBo userBo = redisDao.getUserBo(token);
            if(userBo!=null){
                user.setId(userBo.getUserId());
                return iUserService.updateNickName(user);
            }
        }
        return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
    }

    /**
     * 发送登录验证码
     *
     * @return
     */
    @RequestMapping("/sendVerCode")
    public ServerResponse sendRegistSMS(String phone, HttpServletRequest request) {
        if (phone != null) {
            String ip = getRemoteHost(request);
            if (ip != null) {
                return iUserService.sendSMS(phone, ip);
            }
        }

        return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
    }

    /**
     * 修改头像
     */
    @RequestMapping("/updateHeadImg")
    public ServerResponse uploadImg(@RequestParam(value = "file",required = false) MultipartFile file, HttpServletRequest request){

        String token = request.getHeader(Const.CURRENT_USER);
        if (StringUtils.isNotBlank(token)) {
            //防止横向越权
            UserBo userBo = redisDao.getUserBo(token);
            if(userBo!=null){
                User user = new User();
                user.setId(userBo.getUserId());
                return iUserService.updateHeadImg(file,user);
            }
        }
        return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
    }

    /**
     * 获取客户端真实ip
     *
     * @param request
     * @return
     */
    public String getRemoteHost(javax.servlet.http.HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }
}
